﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using WF_Business;
using ExtBusiness.Interface;
using System.Net;
using WF_DataAccess;
using ExtBusiness;
using ExtBusiness.Common;
using ExtBusiness.FlowOperation;
using System.Configuration;
using ChBusiness.Common;

public partial class office_OnlineNewTask : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BingData();
        }
    }

    protected void BingData()
    {
        String strWorkFlowName = Request["FlowName"];//流程名称
        WorkFlowPubSettting wfps = new WorkFlowPubSettting();

        if (strWorkFlowName.IndexOf("地图审核") > -1)
        {
            if (strWorkFlowName=="地图审核变更审批"||strWorkFlowName=="地图审核延期审批"||strWorkFlowName == "地图审核撤销审批")
            {
                string strWid = wfps.GetWorkWid(strWorkFlowName);
                ListItem liTemp = new ListItem(strWorkFlowName, strWid);
                ddlFlows.Items.Add(liTemp);
                NewTask();
            }
            else//审批流程
            {
                string[] arrFlowName = SystemConfig.SpecialFlowName.Split(',');
                foreach (string strFlow in arrFlowName)
                {
                    //20日
                    string strWid = wfps.GetWorkWid(strFlow); ;
                    ListItem liTemp = new ListItem(strFlow, strWid);
                    ddlFlows.Items.Add(liTemp);
                }
            }
        }
        else
        {
            string strWid = wfps.GetWorkWid(strWorkFlowName);
            ListItem liTemp = new ListItem(strWorkFlowName, strWid);
            ddlFlows.Items.Add(liTemp);
            NewTask();
        }
    }
   

    /// <summary>
    /// 在线申报业务受理
    /// </summary>
    /// <!--addby zhongjian 20091021-->
    public void NewTask()
    {
        //创建流程实例
        WorkFlow work = new WorkFlow();
        string iid = "", wiid = "", errmsg = "";
        String strSql = string.Empty;
        String strWid = ddlFlows.SelectedValue;//选择的流程id
        String strSqr = Request["sqr"]; //申请单位名称
        String strWorkFlowName = ddlFlows.SelectedItem.Text;//流程名称
        String strAppid = Request["iid"];
        iid = strAppid;
        IDataAccess dataAccess = SysParams.OAConnection(true);
        bool bSuccess = false;

        try
        {
            if (string.IsNullOrEmpty(strSqr))
            {
                strSqr = "无";
                //Common.WindowAppear.WriteAlert(this.Page, "申请单位不能为空。");
                //return;
            }

            //特殊流程与申请表不参共享iid
            if (strWorkFlowName == "地图审核变更审批" || strWorkFlowName == "地图审核延期审批" || strWorkFlowName == "地图审核撤销审批")
            {
                iid = "";
            }

            if (true == work.NewInstance(strWid, UserId, strSqr, ref iid, ref wiid, ref errmsg, ref dataAccess))
            {
                //插入工作流实例的扩展信息 
                //--添加业务状态标识:status (0:已受理;2:已办结;-1:返回补证;-2:驳回中止;-3:不予受理;-4:审批不通过)
                int nIsNw = 0;
                if (Request["is_nw"] == "1")//判断是否是内网受理
                {
                    nIsNw = 1;
                }

                //对于从外网申报的主流业务申请，默认关联id和iid相同
                strSql = string.Format(@"insert into xt_instance_ext(iid,申请单位,联系人,关联ID,status,IS_NW) values({0},'{1}','{2}','{3}','0','{4}')", iid, strSqr, UserName, strAppid, nIsNw);
                dataAccess.RunSql(strSql);

                if (strWorkFlowName == "地图审核(即送即审)")
                {
                    strSql = string.Format(@"update st_instance set PRIORITY='2' where iid='{0}'", iid);
                    dataAccess.RunSql(strSql);
                }

                OracleSystemOperation osoTemp = new OracleSystemOperation();
                HandleCaseService hcsTemp = new HandleCaseService();

                //分流程类型来添加表单数据 addby zhongjian 20100311
                switch (strWorkFlowName)
                {
                    case "国家涉密基础测绘成果使用申报":// 将外网申报信息id(strAppid)和内网审批系统中刚刚生成的iid做好对应关系
                       
                        break;
                    case "地图审核变更审批":
                        osoTemp.GetNewTaskSql(iid, strAppid, "UT_国家测绘局地图审核申请表", dataAccess);
                        osoTemp.GetNewTaskSql(iid, strAppid, "UT_地图审核受理通知书", dataAccess);
                        osoTemp.GetNewTaskSql(iid, strAppid, "UT_地图审核变更申请单", dataAccess);

                        hcsTemp.AddSuspend(strAppid, UserId,strWorkFlowName,iid);//挂起对应的流程
                        break;
                    case "地图审核延期审批":
                        osoTemp.GetNewTaskSql(iid, strAppid, "UT_国家测绘局地图审核申请表", dataAccess);
                        osoTemp.GetNewTaskSql(iid, strAppid, "UT_地图审核受理通知书", dataAccess);
                        osoTemp.GetNewTaskSql(iid, strAppid, "UT_地图审核延期申请单", dataAccess);
                        hcsTemp.AddSuspend(strAppid, UserId,strWorkFlowName,iid);//挂起对应的流程
                        break;
                    case "地图审核撤销审批":
                        osoTemp.GetNewTaskSql(iid, strAppid, "UT_国家测绘局地图审核申请表", dataAccess);
                        osoTemp.GetNewTaskSql(iid, strAppid, "UT_地图审核受理通知书", dataAccess);
                        osoTemp.GetNewTaskSql(iid, strAppid, "UT_地图审核撤销申请单", dataAccess);
                        hcsTemp.AddSuspend(strAppid, UserId,strWorkFlowName,iid);//挂起对应的流程
                        break;
                    default:
                        break;
                }

                //添加操作日志 addby zhongjian 20100331
                //获取操作者ip地址
                string myIP = Common.IPOperation.GetIpAddress();
                //操作描述
                string strRemark = "受理申请编号为: " + strAppid + " 受理编号为: " + iid + " 的案件;流程类型为:" + strWorkFlowName + "";
                SystemLogs.AddSystemLogs(UserName, "add", strRemark, "", myIP);

                //先删除已有的附件内容,针对补正补齐重复提交的情况 addby zhongjian 20100326
                strSql = string.Format(@"delete from st_attachment where aid in (select id from XT_SERIAL_ATTACHMENT where AppDataId = '{0}' or (userid='{1}' and IS_TEMPLET=1))", strAppid, Request["SbUserId"]);
                dataAccess.RunSql(strSql);

                //插入受理附件表 addby zhongjian 20091030
                strSql = string.Format(@"insert into st_attachment(aid,magic_number,ext_name,data,name) select id,'1','.'||filetype,filedata,filename from XT_SERIAL_ATTACHMENT where AppDataId='{0}' or (userid='{1}' and IS_TEMPLET=1)", strAppid,Request["SbUserId"]);
                dataAccess.RunSql(strSql);

                //插入受理动态目录表，附件地址按流程类型分类,并添加附件后缀名
                string strAttPath = "扫描资料/申请资料/";
                switch (strWorkFlowName)
                {
                    case "国家涉密基础测绘成果使用申报"://涉密流程处理表单时查看附件的地址.
                         strAttPath = "扫描资料/申请资料/";
                        break;
                    case "地图审核(即送即审)":
                    case "地图审核(5日协审)":
                    case "地图审核(7日)":
                    case "地图审核(20日)":
                        strAttPath = "扫描资料/";
                        break;
                    case "永久性测量标志拆迁审批":
                        strAttPath = "电子附件/";
                        break;
                    case "DTSH_地图审核程序流程图":
                    case "地图审核变更审批":
                    case "地图审核延期审批":
                    case "地图审核撤销审批":
                        strAttPath = "";//根目录为空
                        break;
                    default:
                        break;
                }


                strSql = string.Format(@"insert into st_dynamic_resource(iid,path,sname,userid,type,res_value) select '{0}','{3}'||(select decode(a.modulename,null,'',a.modulename||'/') from xt_serial_module a where a.id = t.module_id)||filename||'.'||filetype,'窗口受理','{1}','2',id from XT_SERIAL_ATTACHMENT t where AppDataId='{2}' or (userid='{4}' and IS_TEMPLET=1)", iid, UserId, strAppid, strAttPath,Request["SbUserId"]);
                dataAccess.RunSql(strSql);

                dataAccess.Close(true);
                bSuccess = true;
            }
            else
            {
                Common.WindowAppear.WriteAlert(this.Page, errmsg);
                dataAccess.Close(false);//回滚
            }
        }
        catch
        {
            dataAccess.Close(false);
            throw;
        }

        if (bSuccess)
        {
            //发送消息
            ClsSubmitInfo clsSubmit = new ClsSubmitInfo();
            clsSubmit.SetMsg(strAppid, "业务受理");

            //打开处理页面,并跳转
            string strScript = string.Format("HandleWorkItem('{0}001','{0}','窗口受理');window.location.href='../{2}?WorkFlowName={1}';", iid,strWorkFlowName,ConfigurationManager.AppSettings["dbsxUrl"]);
            Common.WindowAppear.ExecuteScript(this.Page, strScript);
        }
    }
}
